package api

import (
	"net/http"
	"newsletter-subs/auth"
	"newsletter-subs/pkg/model"
	"newsletter-subs/pkg/service"

	"github.com/gin-gonic/gin"
)

func RegistRouteSubscriptions() {
	subs := RouteEng.Group("/subscription")
	{
		// Routes in this group will be prefixed with /subscription
		subs.GET("/", auth.ValidateToken, GetSubscriptions)
		subs.GET("/:id", auth.ValidateToken, GetOneSubscription)
		subs.POST("/", auth.ValidateToken, CreateSubscription)
		subs.PUT("/:id", auth.ValidateToken, UpdateSubscription)
		subs.DELETE("/:id", auth.ValidateToken, DeleteSubscription)
	}
}

// @Summary 显示所有订阅信息
// @Description 显示所有订阅信息
// @Produce json
// @Security Bearer
// @Success 200 {object} model.Results 成功后返回值
// @Router /subscription/ [GET]
func GetSubscriptions(c *gin.Context) {
	results := service.GetSubscriptions()
	c.JSON(http.StatusOK, results)
}

// @Summary 创建订阅信息
// @Description 创建订阅信息
// @Produce json
// @Security Bearer
// @Param subscription body model.Subscription true "订阅信息"
// @Success 200 {object} model.Result 成功后返回值
// @Router /subscription [post]
func CreateSubscription(c *gin.Context) {
	subscription := &model.MSubscription{}

	c.BindJSON(subscription)
	result := service.CreateSubscription(subscription)
	c.JSON(http.StatusOK, result)
}

// @Summary 更新订阅信息
// @Description 更新订阅信息
// @Produce json
// @Security Bearer
// @Param id path string true "订阅id"
// @Param subscription body model.Subscription true "订阅信息"
// @Success 200 {object} model.Result 成功后返回值
// @Router /subscription/{id} [put]
func UpdateSubscription(c *gin.Context) {
	subscription := &model.MSubscription{}
	id, _ := c.Params.Get("id")
	// idn, _ := strconv.Atoi(id)

	c.BindJSON(subscription)
	result := service.UpdateSubscription(id, subscription)
	c.JSON(http.StatusOK, result)
}

// @Summary 删除订阅信息
// @Description 删除订阅信息
// @Produce json
// @Security Bearer
// @Param id path string true "订阅id"
// @Success 200 {string} json "{"code":200,"data":"deleted","msg":"ok"}"
// @Router /subscription/{id} [delete]
func DeleteSubscription(c *gin.Context) {
	id, _ := c.Params.Get("id")
	// idn, _ := strconv.Atoi(id)

	service.DeleteSubscription(id)
}

// @Summary 显示订阅信息
// @Description 显示订阅信息
// @Produce json
// @Security Bearer
// @Param id path string true "订阅id"
// @Success 200 {object} model.Result 成功后返回值
// @Router /subscription/{id} [get]
func GetOneSubscription(c *gin.Context) {
	id, _ := c.Params.Get("id")
	// idn, _ := strconv.Atoi(id)

	result := service.GetOneSubscription(id)
	c.JSON(http.StatusOK, result)
}
